import api from '../utils/api'
import {httpRequest} from '../utils/utils'
export const configOptions = {
    isReady:false,
    options:{
        env:"test"
    } 
}
const urlMap = new Map()
urlMap.set('test','https://yjhihis-test.yjhealth.cn')
urlMap.set('pre','https://pre-hicrm.healthyj.cn')
urlMap.set('prod','https://crm.healthyj.cn')
export default async function config(opts={env:'test'},cb){
    configOptions.options = opts ? Object.assign(configOptions.options,opts):configOptions.options; 
    configOptions.options.url = urlMap.get(configOptions.options.env) 
    if(!(opts.idPi || opts.token)){
        console.error('you must provide "idPi" or "token" params in config function')   
        return;
    }else{
        if(opts.idPi){
            const res = await httpRequest(api.getToken,{
                method:'post',
                body:{idPi:opts.idPi}
            })
            if(res && res.body){
                configOptions.options.token = res.body
            }
        } 
        if(configOptions.options.token){
            if(cb && typeof cb == 'function'){
                configOptions.isReady = true
                configOptions.isReady && cb.call(this);
            }
        }
    }
    return this
}